import {patchZero} from '@/utils'
import {ElLoading} from "element-plus";

export function getWeek(weekIndex) {
    const weekEnum = {
        '0': '周日',
        '1': '周一',
        '2': '周二',
        '3': '周三',
        '4': '周四',
        '5': '周五',
        '6': '周六',
    }
    return weekEnum[weekIndex]
}


export function getRandomColor(index) {
    const colorList = ['#1abc9c', '#16a085', '#9c88ff', '#00a8ff', '#7d5fff', '#34495e', '#12CBC4',
        '#2ecc71', '#27ae60', '#3498db', '#2980b9', '#a55eea', '#f78fb3',
        '#9b59b6', '#8e44ad', '#f1c40f', '#f39c12', '#e67e22', '#d35400', '#c56cf0', '#546de5'
    ]
    return colorList[index] || colorList[0]
}

export function patchProject(weekList, projectList, username, id, color, itemHeight = 88) {
    if (!projectList || !projectList.length) {
        const resp = {
            name: username,
            id,
            height: itemHeight,
        }
        weekList.forEach(item => {
            resp[item.value] = [{left: 0, top: 0, empty: true}]
        })
        return {
            height: itemHeight,
            result: resp
        }
    }
    const countObj = {}
    const list = projectList.reduce((pre, cur) => {
        if (!cur) {
            cur = []
        }
        pre.push(...cur.map(item => {
            const objKey = item.date.replace(/\-/g, '_')
            if (countObj.hasOwnProperty(objKey)) {
                countObj[objKey] += 1
            } else {
                countObj[objKey] = 1
            }
            item.color = color
            return item
        }))
        return pre
    }, [])
    const max = Math.max(...Object.values(countObj)) || 0
    const result = {name: username, id}
    weekList.forEach(item => {
        const v = list.filter(jobItem => jobItem.date === item.week).map((jobItem, index) => {
            const {is_jiaban, is_yanqi} = jobItem
            jobItem.is_jiaban = !!(is_jiaban * 1)
            jobItem.is_yanqi = !!(is_yanqi * 1)
            jobItem.top = index * itemHeight
            jobItem.left = 0
            jobItem.empty = false
            return jobItem
        })
        const startLen = v.length
        for (let i = startLen; i < max; i++) {
            v.push({
                left: 0,
                top: i * itemHeight,
                empty: true,
                is_jiaban: false,
                is_yanqi: false,
            })
        }
        result[item.value] = v
    })
    const heightValue = max <= 0 ? itemHeight : itemHeight * max
    result.height = heightValue
    return {
        height: heightValue,
        result,
    }
}

export function formatDate(timestamp, dateStr) {
    const date = new Date(timestamp);
    const [year, month, day] = [date.getFullYear(), date.getMonth() + 1, date.getDate()]
    const label = `${('0' + month).slice(-2)}-${('0' + day).slice(-2)}`
    const week = `${year}-${label}`
    const weekStr = getWeek(date.getDay())
    const active = week === dateStr
    return {
        active,
        week,
        label,
        weekText: weekStr,
        value: date.getTime()
    }
}

export function getWeekList(startNum, timestamp, todayStr) {
    const date = new Date(timestamp)
    const startDate = date.setDate(date.getDate() - startNum)
    const result = []
    const oneDay = 1000 * 60 * 60 * 24
    for (let i = 0; i < 20; i++) {
        result.push(formatDate(startDate + oneDay * i, todayStr))
    }
    console.log(result, 'result')
    return result
}

export function getDateStr(date) {
    const [year, month, day] = [date.getFullYear(), date.getMonth() + 1, date.getDate()]
    return `${year}-${patchZero(month)}-${patchZero(day)}`
}

let loadingTarget = null

export function showLoading(target) {
    if (!loadingTarget) {
        loadingTarget = ElLoading.service({
            target,
            text: '正在获取数据',
        })
    }
}

export function hideLoading() {
    loadingTarget && loadingTarget.close()
    loadingTarget = null
}
